<template>
  <div class="main_box">
    <el-row>
      <el-col :span="12">
        <Crumbs></Crumbs>
      </el-col>
      <el-col :span="12">
        <div class="opration_wrap">
          <el-button type="primary" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
        </div>
      </el-col>
    </el-row>
    <el-row>
      <div class="search_wrap">
        <el-form :inline="true" :model="searchForm" ref="searchForm">
          <el-form-item prop="statYear">
            <el-select v-model="searchForm.statYear" placeholder="请选择年份" :clearable="true">
              <el-option v-for="item in yearList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="statMonth">
            <el-select v-model="searchForm.statMonth" placeholder="请选择月份" :clearable="true">
              <el-option v-for="item in monthList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="peerNumber">
            <el-input v-model="searchForm.peerNumber" placeholder="对方号码" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="onSearch">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-row>
    <el-row ref="tableRow">
      <el-table :data="tableList" :fit="true">
        <el-table-column type="index" label="序号" width="50"></el-table-column>
        <el-table-column label="客户姓名" align="center">
          <template>
            <span>{{ this.$route.query.name }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="billMonth" label="通话月份" align="center"></el-table-column>
        <el-table-column prop="time" label="通话时间" align="center"></el-table-column>
        <el-table-column prop="peerNumber" label="对方号码" align="center"></el-table-column>
        <el-table-column prop="location" label="本机通话地" align="center"></el-table-column>
        <el-table-column prop="locationType" label="通话地类型" align="center"></el-table-column>
        <el-table-column prop="duration" label="通话时长(秒)" align="center"></el-table-column>
        <el-table-column prop="dialType" label="呼叫类型" align="center" :formatter="formatDialType"></el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>
  </div>
</template>
<script>
import Crumbs from '@/components/Crumbs';
import * as message from '@/constants/message';
import { FORMAT_DATE_YYYY_MM_DD } from '@/constants/constants';

export default {
  data() {
    return {
      searchForm: {
        statYear: '',
        statMonth: '',
        peerNumber: '',
      },
      page: {
        pageNo: 1,
        pageSize: 20,
        pageTotal: 0,
        currentPage: 1,
      },
      tableList: [],

      yearList: [
        { value: '2018', label: '2018年' },
        { value: '2019', label: '2019年' },
        { value: '2020', label: '2020年' },
        { value: '2021', label: '2021年' },
        { value: '2022', label: '2022年' },
        { value: '2023', label: '2023年' },
        { value: '2024', label: '2024年' },
        { value: '2025', label: '2025年' },
      ],
      monthList: [
        { value: '01', label: '01月' },
        { value: '02', label: '02月' },
        { value: '03', label: '03月' },
        { value: '04', label: '04月' },
        { value: '05', label: '05月' },
        { value: '06', label: '06月' },
        { value: '07', label: '07月' },
        { value: '08', label: '08月' },
        { value: '09', label: '09月' },
        { value: '10', label: '10月' },
        { value: '11', label: '11月' },
        { value: '12', label: '12月' },
      ],
    };
  },
  components: {
    Crumbs,
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      this.listTable();
      this.getTableHeight();
    },

    formatDialType(row, column, cellValue) {
      if (cellValue == 'DIAL') {
        return '主叫';
      } else if (cellValue == 'DIALED') {
        return '被叫';
      }
    },
    listTable() {
      if (this.searchForm.statMonth != '') {
        if (this.searchForm.statYear == null || this.searchForm.statYear == '') {
          this.$message({
            message: '请选择年份',
            type: 'error',
          });
          return;
        }
      }
      let data = {
        pageNo: this.page.pageNo,
        pageSize: this.page.pageSize,
        customerId: this.$route.query.id,
        statYear: this.searchForm.statYear,
        statMonth: this.searchForm.statMonth,
        peerNumber: this.searchForm.peerNumber,
      };
      this.fetchData(data);
    },
    handleSizeChange(size) {
      this.page.pageSize = size;
      this.listTable();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.listTable();
    },
    onSearch() {
      this.page.pageNo = 1;
      this.listTable();
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    fetchData(data) {
      this.$emit('show-loading', true);
      this.$Get(this.$Config.api.dataCallList, data)
        .then((res) => {
          this.tableList = res.data.list;
          this.page.pageTotal = res.data.total;
          this.page.currentPage = res.data.pageNum;
          this.$emit('show-loading', false);
        })
        .catch((error) => {
          console.error(error);
          this.$emit('show-loading', false);
        });
    },
    returnList() {
      this.$router.push({ name: '客户列表' });
    },
  },
};
</script>
<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
